/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m006.bl;

import ep.persistence.controllers.T006PrescriptionJpaController;
import ep.persistence.entities.T006Prescription;
import java.util.Collection;
import javax.persistence.Query;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author Home
 */
@Path("bl006")
public class BL006Controller {

    @GET
    @Path("/finalized")
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Collection<T006Prescription> getFinalizedPrescritions(@QueryParam("patientId") String patientId) {
        T006PrescriptionJpaController jpaController = new T006PrescriptionJpaController();
        String queryString = "select distinct p.* "
                + "from t006_prescription p "
                + "inner join t008_dosage d on d.t006_presciption_id = p.id "
                + "where d.prescribed = 1 "
                + "and p.t005_patient_id = ? ";
        Query query = jpaController.getEntityManager().createNativeQuery(queryString, T006Prescription.class);
        query.setParameter(1, patientId);
        return query.getResultList();
    }

    @GET
    @Path("/unprescriped")
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Collection<T006Prescription> getUnprescripedPrescriptions(@QueryParam("patientId") String patientId) {
        T006PrescriptionJpaController jpaController = new T006PrescriptionJpaController();
        String queryString = "select distinct p.* "
                + "from t006_prescription p "
                + "inner join t008_dosage d on d.t006_presciption_id = p.id "
                + "where (d.prescribed is null or d.prescribed = 0) "
                + "and p.t005_patient_id = ? ";
        Query query = jpaController.getEntityManager().createNativeQuery(queryString, T006Prescription.class);
        query.setParameter(1, patientId);
        return query.getResultList();
    }
}
